package com.xuxu.blog.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuxu.blog.dao.ArticleDao;
import com.xuxu.blog.entity.ArticleEntity;
import com.xuxu.blog.service.ArticleService;
import com.xuxu.common.utils.PageUtils;
import com.xuxu.common.utils.Query;
import com.xuxu.common.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service("articleService")
@Slf4j
public class ArticleServiceImpl extends ServiceImpl<ArticleDao, ArticleEntity> implements ArticleService {
    @Autowired
    ArticleDao articleDao;
    @Autowired
    DictServiceImpl dictService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ArticleEntity> page = this.page(
                new Query<ArticleEntity>().getPage(params),
                new QueryWrapper<ArticleEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage2(Map<String, Object> params) {
        String author = (String) params.get("author");
        IPage<ArticleEntity> page = this.page(
                new Query<ArticleEntity>().getPage(params),
                new QueryWrapper<ArticleEntity>().orderByDesc("id").eq("author", author)
        );

        return new PageUtils(page);
    }

    @Override
    public List<ArticleEntity> queryList() {
        List<ArticleEntity> list = articleDao.selectList(null);
        return list;
    }

    @Override
    public R selectArticleByFlag() {
        List<ArticleEntity> articleEntities = articleDao.selectList(null);
        return R.ok().put("data",articleEntities);
    }

    public void SaveArticle(ArticleEntity article) {
        if (StrUtil.isBlank(article.getImg())) {
            article.setImg("http://tamad.fun:9001/images/2022/08/24/1219a2e9af874169a902f19f24bb5c85.jpg");
        }
        articleDao.insert(article);
        // 判断dist数据表中是否有该标签 ，此处进行了同步
        // dictService.DetectExistsAndInsert(article);
    }

    @Override
    public R queryByAuthor(String author) {
        List<ArticleEntity> articleEntityList;
        log.warn("用户信息是：{}",author);
        if ("all".equals(author)){
            articleEntityList = articleDao.selectGroup();

        }else {
            articleEntityList = articleDao.selectGroupByAuthor(author);
        }
        return R.ok().put("data",articleEntityList);
    }

    /**
     * 通过作者和标题查询博客信息
     *
     * @param author
     * @param category
     * @return
     */
    @Override
    public R queryByAuthorCategory(String author, String category) {
        QueryWrapper<ArticleEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("author", author);
        wrapper.eq("category", category);
        List<ArticleEntity> list = articleDao.selectList(wrapper);
        return R.ok().put("data", list);
    }

    @Override
    public R getCount() {
        List<ArticleEntity> list = articleDao.selectList(null);
        long count = list.stream().count();
        return R.ok().put("data",count);
    }

    @Override
    public R getCounts() {
        Integer countInteger = articleDao.getCountInteger();
        return R.ok().put("data", countInteger);
    }

    @Override
    public R selectArticleByAuthor(String author,Integer current,Integer size) {
        QueryWrapper<ArticleEntity> wrapper = new QueryWrapper<>();
        // wrapper.select("author", author);
        wrapper.eq("author", author);
        Page<ArticleEntity> list = articleDao.selectPage(new Page<>(0, 5), wrapper);
        return R.ok().put("data",list.getRecords());
    }

    @Override
    public R getTags(String author) {
        List<ArticleEntity> byTags = articleDao.getByTags(author);
        return R.ok().put("data",byTags);
    }

    @Override
    public R selectNewByAuthor(String author) {
        List<ArticleEntity> list = articleDao.selectNewByAuthor(author);
        return R.ok("查询前10条数据").put("data", list);
    }

    @Override
    public R queryPageDesc(String author,String current,String size) {
        Integer current2 = Integer.valueOf(current);
        Integer size2 = Integer.valueOf(size);
        QueryWrapper<ArticleEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("author", author).orderByDesc("id");
        Page<ArticleEntity> page = new Page<>(current2,size2);
        Page<ArticleEntity> selectPage = articleDao.selectPage(page, wrapper);
        return R.ok("倒序分页展示文章").put("data", selectPage);
    }

    @Override
    public R querySelfPageDesc(String current, String size) {
        Integer current2 = Integer.valueOf(current);
        Integer size2 = Integer.valueOf(size);
        QueryWrapper<ArticleEntity> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id");
        wrapper.eq("flag", "1");
        Page<ArticleEntity> page = new Page<>(current2,size2);
        Page<ArticleEntity> selectPage = articleDao.selectPage(page, wrapper);
        return R.ok("倒序分页展示文章").put("data", selectPage);
    }

}